﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using Microsoft.ApplicationBlocks.Data;
using Dimok.Data.Database;
using Dimok.Data;
using Tiffer;

public class UOX : UserObjectEx
{
    // Methods
    public int AddApplicant(string closingFile)
    {
        return (int) SQLHelperEx.ExecSP(db, "s_AddApplicant", new object[] { closingFile });
    }

    public byte[] ImageFolder(int folderID)
    {
        return this.ImageFolder(applID, folderID);
    }

    public void ImageFolder(int folderID, byte[] image)
    {
        this.ImageFolder(applID, folderID, image);
    }

    public byte[] ImageFolder(int applID, int folderID)
    {
        object obj2 = SQLHelperEx.ExecuteScalar(db, CommandType.Text, string.Concat(new object[] { "SELECT Data FROM t_Image WHERE ApplicantID=", applID, " AND ImageTypeID=", folderID }));
        return (((obj2 == DBNull.Value) || (obj2 == null)) ? new byte[0] : ((byte[]) obj2));
    }

    public void ImageFolder(int applID, int folderID, byte[] image)
    {
        SqlDataAdapter adapter = new SqlDataAdapter(string.Concat(new object[] { "SELECT * FROM t_Image WHERE ApplicantID=", applID, " AND ImageTypeID=", folderID }), db);
        SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
        DataSet dataSet = new DataSet();
        adapter.Fill(dataSet, "Table");
        DataTable table = dataSet.Tables["Table"];
        if (table.Rows.Count == 0)
        {
            SqlHelper.ExecuteNonQuery(db, "s_Image_Update", new object[] { applID, folderID });
            this.ImageFolder(applID, folderID, image);
        }
        else
        {
            if ((image == null) || (image.Length == 0))
            {
                table.Rows[0]["Data"] = DBNull.Value;
            }
            else
            {
                table.Rows[0]["Data"] = image;
            }
            adapter.Update(dataSet, "Table");
        }
    }

    public byte[] ImageFolder(int applID, int folderID, int pageStart, int pageCount)
    {
        byte[] buffer = this.ImageFolder(applID, folderID);
        if ((buffer != null) && (buffer.Length != 0))
        {
            return Tiff.GetPages(buffer, pageStart, pageCount);
        }
        return new byte[0];
    }

    // Properties
    public override int applID
    {
        get
        {
            return base.applID;
        }
        set
        {
            base.applID = value;
        }
    }

    public int coApplID
    {
        get
        {
            return (int) SQLHelperEx.ExecSP(db, "s_Applicants_Get_CoX", new object[] { applID, 1 });
        }
    }

    public int hasCoApplID
    {
        get
        {
            return (int) SQLHelperEx.ExecSP(db, "s_Applicants_Get_CoX", new object[] { applID });
        }
    }

    public SqlDataReader pages
    {
        get
        {
            return SqlHelper.ExecuteReader(db, CommandType.Text, "SELECT * FROM v_Menu_Pages WHERE MenuID=" + menuID+ " ORDER BY TabIndex");
        }
    }

    public DataSet preApproval
    {
        get
        {
            return SqlHelper.ExecuteDataset(db, CommandType.Text, "SELECT * FROM vx_PreApprove WHERE ApplicantID=" + applID);
        }
    }

    public DataSet preApproval_BorrowerInfo
    {
        get
        {
            return SqlHelper.ExecuteDataset(db, "s_PreApprove_BorrowerInfo", new object[] { applID });
        }
    }
}

